package activiti

//import base.OpenJs
import grails.converters.JSON
import grails.plugin.springsecurity.annotation.Secured

//import grails.transaction.Transactional

//@Transactional(readOnly = true)
@Secured("IS_AUTHENTICATED_FULLY")
class ActSolutionController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
    def utilsService
    def activitService
    def actSolutionService

    def saveOrUpdate() {
        def result = [success: true]
        actSolutionService.saveOrUpdate(params)
        render result as JSON
    }

    //  表单下拉
//    def formCombo() {
//        def hql = "select js from OpenJs js left join  js.lclx lx  where 1=:one"
//        def hmap = [one: 1]
//        if (params.isStartTask.toString() == '1') {
//            hql += "  and  js.fqActionUrl is not null "
//            if (utilsService.validString(params.typeCode)) {
//                hql += "  and lx.code=:typeCode"
//                hmap << [typeCode: params.typeCode]
//            }
//        } else {// 不是发起就是审核环节
//            hql += "  and js.type3='sh' "
//            if (utilsService.validString(params.typeCode)) {
//                hql += "  and (lx.code=:typeCode  or lx is null )"
//                hmap << [typeCode: params.typeCode]
//            }
//        }
//
//        log.debug(hql + "  " + hmap)
//        def list = OpenJs.executeQuery(hql, hmap)
//        log.debug(list)
//        def result = [root: []]
//        list.each {
//            result.root << [id: it.id, name: it.jsName + " " + it.description, formName: it.jsName, fqActionUrl: it.fqActionUrl]
//        }
//        render result as JSON
//    }

    def getMxById() {
        def list = ActSolutionMx.findAll("from ActSolutionMx  where actSolution.id=:id order by  id ", [id: (params.id ?: 0) as long])
        def result = [success: true, gridroot: []]
        result.gridroot = ActSolutionMx.newInstance().buildDomainList(list)
        render result as JSON
    }
    // 据 流程模型(流程定义) 重新加载 环节信息,据已保存的 方案明细 把已保存的数据是带出  入参: 流程定义的id (modelId),方案id (actSolutionId)
    //   http://localhost:8010/prm/actSolution/reloadMxByModel?modelId=15011&actSolutionId=0
    def reloadMxByModel() {
        log.debug(params)
        def result = [success: true, gridroot: []]
        if (utilsService.validString(params.modelId))
            result = activitService.getTasksFromModel([id: params.modelId])
        if (utilsService.isNumber(params.actSolutionId) && params.actSolutionId?.toString() != '0') {
            def mxList = ActSolutionMx.findAll("from ActSolutionMx  where actSolution.id=:id ", [id: (params.actSolutionId ?: 0) as long])
            result.gridroot.each { modelmx ->
                def mx = mxList.find { it.taskId == modelmx.taskId }
                if (mx){
                    def taskId=modelmx.taskId
                    def taskName=modelmx.taskName
                    modelmx.putAll(mx.buildDomain())
                    modelmx.taskId=taskId
                    modelmx.taskName=taskName
                }


            }
        }
        render result as JSON
    }

    def delMany() {
        log.debug(params)
        def result = [success: true]
        result = utilsService.deleteMany(params, ActSolution, ['新建'], '业务方案')
        render result as JSON
    }
    // 部署方案,同时 部署 流程定义
    def deploy() {
        log.debug(params)
        params.request = request
        def result = [success: true]
        actSolutionService.deploy(params)
        render result as JSON
    }
    def findlist = {
        if (utilsService.validString(params.status))
            params.remove('allowStatus')
        def domain = controllerName[0].toUpperCase() + controllerName[1..controllerName.length() - 1]
        def Instance = grailsApplication.classLoader.loadClass('activiti.' + domain)
        def result = utilsService.commonlist(Instance, params)
        render result as JSON
    }
    // 据 流程的piid 得到打印的actionName(再reporterContro中),返回一个列表,开发者据顺序来判断应该应该时第几个打印  /prm/actSolution/getPrintUrl
    def getPrintUrl(){
        def list=ActSolutionMx.findAll("from ActSolutionMx where  actSolution.actModel.id  in (select  modelId from  CommonTask   where piid=:piid and modelId is not  null)",[piid:params.piid]).printAction
        def result=[]
        list.each {
            if (it.toString().startsWith("/")) result<<it
            else result<<("/"+it)
        }
        render result
    }
    // 批量开通APP 功能
    def ktAP(){
        def result = [success: true]
        if(params.ids) {
            params.syApp = true
            actSolutionService.changeSyApp(params)
        }
        render result as JSON
    }
    // 批量  禁用APP
    def jyAPP(){
        def result = [success: true]
        if(params.ids) {
            params.syApp = false
            actSolutionService.changeSyApp(params)
        }
        render result as JSON
    }
}
